﻿using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Bingosoft.Modules.SecurityConsole.Common.Config;
using Bingosoft.Modules.SecurityConsole.Organization;
using Bingosoft.Web.Utils;

namespace Bingosoft.WebApp.Modules.SecurityConsole.Organization
{
    public partial class OrganizationList : System.Web.UI.BasePage
    {
        private OrganizationService _organizationService;
        private OrganizationService OrganizationService
        {
            get
            {
                return _organizationService ??
                       (_organizationService = ObjectHelper.GetObject<OrganizationService>());
            }
        }

        protected String parent;
        protected String parentName;
        protected void Page_Load(object sender, EventArgs e)
        {
            parent = Request["Parent"];
            parentName = Request["ParentName"];
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(parent))
                {
                    divToolbar.Visible = false;
                }
            }
        }

        protected void RefreshGrid(object sender, EventArgs e)
        {
            //this.GridSearch.Search(sender, e);
        }

        protected void DelOrg(object sender, EventArgs e)
        {
            var _sender = sender as ImageButton;
            if (_sender != null)
            {
                String orgId = _sender.Attributes["_KeyId"];

                this.OrganizationService.DeleteOrganization(orgId);
            }

            RefreshGrid(sender, e);

            ScriptHelper.RegisterScript(this, "window.parent.reloadTree();");
        }
        protected void EnableOrg(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            string orgId = img.Attributes["_KeyId"];
            this.OrganizationService.EnableOrg(orgId);
            this.GridSearch.Refresh();
        }

        protected void DisableOrg(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            string orgId = img.Attributes["_KeyId"];
            this.OrganizationService.DisableOrg(orgId);
            this.GridSearch.Refresh();
        }

        protected void SmartGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton enableImgBtn = e.Row.FindControl("EnableImageButton") as ImageButton;
                ImageButton disableImgBtn = e.Row.FindControl("DisableImageButton") as ImageButton;
                if (enableImgBtn != null)
                {
                    string status = enableImgBtn.Attributes["_STATUS"];
                    if (Constants.ORG_STATUS_ENABLED.Equals(status))
                    {
                        enableImgBtn.Visible = false;
                        if (disableImgBtn != null) disableImgBtn.Visible = true;
                    }
                    else
                    {
                        enableImgBtn.Visible = true;
                        if (disableImgBtn != null) disableImgBtn.Visible = false;
                    }
                }
                if (string.IsNullOrEmpty(parent))
                {
                    HtmlTableCell btnDelete = (HtmlTableCell)e.Row.Cells[3].FindControl("tdDelete");
                    btnDelete.Visible = !string.IsNullOrEmpty(btnDelete.Attributes["_ParentId"]);
                }
            }
        }
    }
}